package com.wxl.common.base

import android.app.Activity
import android.view.View
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.viewmodel.AbsPresenter

abstract class WorkPresenter : AbsPresenter() {

    companion object {
        fun <P : WorkPresenter> getWork(
            context: AppCompatActivity,
            work: Class<P>,
            rootView: View,
            callback: Works.Companion.OnWorkCallback
        ): P {
            return ViewModelQuick.observer(
                context,
                work,
                rootView,
                callback
            )
        }
    }


    open lateinit var rootView: View

    private var callback: Works.Companion.OnWorkCallback? = null

    override fun onCreated(vararg args: Any) {
        rootView = args[0] as View
        callback = args[1] as Works.Companion.OnWorkCallback
    }

    fun finish() {
        if (rootView.context is Activity) {
            (rootView.context as Activity).finish()
        }
    }

    @ColorInt
    fun getResColor(resColor: Int): Int {
        return ContextCompat.getColor(rootView.context, resColor)
    }

    abstract fun onStartDoWork(vararg args: Any)

    fun doNext(vararg args: Any) {
        callback?.onNextWork(*args)
    }
}